// components/time/time.js
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        color: {
            type: String,
            value: "#fff"
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        timeText: ''
    },

    lifetimes: {
        // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
        attached: function () { 
            
        },
        ready: function () {
            //获取时间
            this.getTimeText();
        },
        moved: function () { 
            //console.log("moved")
        },
        detached: function () { 
            //console.log("detached")
        },
    },

    /**
     * 组件的方法列表
     */
    methods: {
        getTimeText(){
            let date = new Date();
            this.setData({
                timeText: this.formatTime(date)
            });
        },
        formatTime(date){
            const year = date.getFullYear();
            const month = date.getMonth() + 1;
            const day = date.getDate();
            return `${[year, month, day].map(this.formatNumber).join('-')}`;
        },
        formatNumber(n){
            n = n.toString();
            return n[1] ? n : `0${n}`;
        }
    }
})
